﻿using DAL.Entity;
using DAL.Persistencia;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NewTierra.Pages.Operacoes
{
    public partial class AddTourMap : System.Web.UI.Page
    {
        public int QuotationGroupID
        {
            get
            {
                try
                {
                    if (Convert.ToInt32(Session["SSIdQuotGrupo"]) <= 0)
                        return 0;

                    return Convert.ToInt32(Session["SSIdQuotGrupo"]);
                }
                catch (Exception)
                {
                    return 0;
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (QuotationGroupID == 0)
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "er", "window.close()", true);
        }

        protected void btnAddTourMap_Click(object sender, EventArgs e)
        {
            if (QuotationGroupID != 0)
            {
                HttpFileCollection hfc = Request.Files;

                if (hfc.Count > 0)
                {
                    Quotation_TourMap qtm = new Quotation_TourMap();

                    if ((fupAddTourMap.FileName.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase)) ||
                        (fupAddTourMap.FileName.EndsWith(".jpeg", StringComparison.OrdinalIgnoreCase)) ||
                        (fupAddTourMap.FileName.EndsWith(".png", StringComparison.OrdinalIgnoreCase)))
                    {
                        HttpPostedFile hpf = hfc[0];

                        if (hpf.ContentLength > 0)
                        {
                            string foto = Convert.ToString(Guid.NewGuid().ToString("n") + System.IO.Path.GetExtension(fupAddTourMap.FileName));
                            hpf.SaveAs(Server.MapPath("~/Galeria/TourMaps/" + foto));

                            Quotation_TourMapDAL qtmd = new Quotation_TourMapDAL();

                            if (qtmd.ObterPorQuotationGrupoID(QuotationGroupID) != null)
                            {
                                qtm = qtmd.ObterPorQuotationGrupoID(QuotationGroupID);

                                if (File.Exists(Server.MapPath("/Galeria/TourMaps/" + qtm.tourMap_Path)))
                                    File.Delete(Server.MapPath("/Galeria/TourMaps/" + qtm.tourMap_Path));
                                qtm.tourMap_Path = foto;

                                qtmd.Atualizar(qtm);
                            }
                            else
                            {
                                qtm.tourMap_Path = foto;
                                qtm.tourMap_QuotationGroup_ID = QuotationGroupID;
                                qtmd.Adicionar(qtm);
                            }
                        }
                    }
                    else
                    {
                        throw new Exception("Erro. Selecione apenas imagens jpg ou png.");
                    }
                }

                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "a",
                    "alert('Foto adicionada com sucesso!'); window.close()", true);
            }
            else
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "a",
                    "alert('O file extra não foi encontrado! Favor tentar novamente'); window.close()", true);
        }
    }
}